<template>
  <!-- 三角洲行动 自动获取授权 -->
  <el-dialog :title="'扫码授权'" :visible.sync="dialogVisible" :close-on-click-modal="false" width="300px" append-to-body @close="close">
    <div class="form-box" v-loading="loading">
      <div class="demo-ruleForm">
        <img :src="qrCode" alt="" style="width: 200px; height: 200px" />
        <div class="center">
          {{
            msg == "二维码认证中" ? "已扫码，等待授权" : "请扫描二维码进行授权"
          }}
        </div>
        <div><el-button style="width: 150px;" @click="checkQrcode" type="primary">我已授权</el-button></div>
      </div>
    </div>
  </el-dialog>
</template>
  
  <script>
import { SanJiaoZhouGetCode,SanJiaoZhouCheckCode,SanJiaoZhouGetData } from "../../api/autoProduction/information";
export default {
  name: "SanJiaoZhou",
  props: [""],
  data() {
    return {
      dialogVisible: false,
      loading: false,
      checkPending: false,
      msg: "请扫描二维码进行授权",
      qrCode: "",
      uuid: "",
      carrierId: "",
      timeOutTimer: null,
      loginBody: "",
    };
  },
  created() {},
  beforeDestroy() {
    this.closeTimer();
  },
  methods: {
    // 打开弹窗
    open(carrierId) {
      this.dialogVisible = true;
      this.loading = false;
      this.carrierId = carrierId;
      this.qrCode = "";
      this.getQrCode();
    },
    close() {
      this.closeTimer();
    },
    getQrCode() {
      this.loading = true;
      this.closeTimer();
      // QQ
      SanJiaoZhouGetCode()
          .then((res) => {
            this.loading = false;
            if (res.code == 200) {
              if(res.data.code==200&&res.data.data){
                this.qrCode =res.data.data.qr_code_base64
                console.log("%c Line:62 🍆 res.data.data", "color:#ea7e5c", res.data.data);
                this.uuid = res.data.data.qr_id;
                this.msg = "";
                if (!this.checkPending) this.checkQrcode();
              }
              // // this.qrCode = res.data.base64Img
              // this.qrCode = "data:image/png;base64," + res.data.qrcode;
              // this.uuid = res.data.qrsig;
              // this.msg = "";
              
            }
          })
          .catch((err) => {
            this.loading = false;
          });
      // if (this.carrierId == 42198) {
        
      // } else if (this.carrierId == 42199) {
      //   // 微信
      //   autoApi
      //     .HYRZGetWxQrcode()
      //     .then((res) => {
      //       this.loading = false;
      //       if (res.code == 200) {
      //         // this.qrCode = res.data.base64Img
      //         this.qrCode = "data:image/png;base64," + res.data.qrcode;
      //         this.uuid = res.data.qrsig;
      //         this.msg = "";
      //         if (!this.checkPending) this.checkQrcode();
      //       }
      //     })
      //     .catch((err) => {
      //       this.loading = false;
      //     });
      // }
    },
    // 关闭延时器
    closeTimer() {
      if (this.timeOutTimer) clearTimeout(this.timeOutTimer);
      this.timeOutTimer = null;
    },
    // 校验二维码状态
    async checkQrcode() {
      let res;
      this.checkPending = true;
       // QQ大区
       try {
          res = await SanJiaoZhouCheckCode({
            qrId: this.uuid,
          });
        } catch (e) {
          if (this.dialogVisible) {
            this.getQrCode();
          }
        }
      // if (this.carrierId == 42198) {
       
      // } else if (this.carrierId == 42199) {
      //   // 微信
      //   try {
      //     res = await autoApi.HYRZCheckWXqrcode({
      //       uuid: this.uuid,
      //     });
      //   } catch (e) {
      //     if (e == "二维码已失效" && this.dialogVisible) {
      //       this.getQrCode();
      //     }
      //   }
      // }
      this.checkPending = false;
      this.closeTimer();
      if (res && res.code==200) {
        if (
          this.dialogVisible &&
          res.data.data.status == 'success'
        ) {
          // 已授权  开始获取数据
          this.loginBody = res.data.data.login_info
          // if (this.carrierId == 42198) {
          //   // QQ大区
          //   this.loginBody = this.extractUrlFromString(res.data);
          // } else if (this.carrierId == 42199) {
          //   // 微信
          //   this.loginBody = this.getWXloginCode(res.data) ;
          // }
          this.getInfo();
        } else if (
          this.dialogVisible &&
          res.code == 200 &&
          (res.msg == "二维码认证中" || res.data.data.status == 'waiting')
        ) {
          this.msg = res.data.data.msg
          // 延时1秒后查询二维码状态
          this.timeOutTimer = setTimeout(() => {
            if (this.dialogVisible) {
              this.checkQrcode();
            }
          }, 2000);
        }else if(res.data.data.status == 'expired'&& this.dialogVisible){
          this.getQrCode();
        }
      }
    },
    async getInfo() {
      this.loading = true;
      let res;
      try {
        // QQ大区
        res = await SanJiaoZhouGetData(this.loginBody);
        // if (this.carrierId == 42198) {
          
        // } else if (this.carrierId == 42199) {
        //   // 微信
        //   res = await autoApi.HYRZwxLogin({
        //     code: this.loginBody,
        //   });
        // }
      } catch (e) {
        this.getQrCode();
      }
      this.loading = false;
      this.closeTimer();
      if (res.data.data.data) {
        this.$emit("success",res.data.data.data);
        this.dialogVisible = false;
      }
    },
    extractUrlFromString(str) {
      // 匹配以http或https开头的URL
      const urlPattern = /https?:\/\/[^\s',]+/;
      const match = str.match(urlPattern);
      return match ? match[0] : null;
    },

    // getWXloginCode(str) {
    //   const regex = /wx_code='([^']+)'/; // 匹配单引号内的内容
    //   const match = str.match(regex);
    //   const wxCode = match ? match[1] : null; // 提取捕获组
    //   return wxCode
    // },
  },
};
</script>
  <style scoped lang="css">
.demo-ruleForm {
  text-align: center;
}
</style>
  